package cn.usts.edu.lesson04;

import javax.swing.*;
import java.awt.*;

public class JIconDemo extends JFrame implements Icon {
    // 设置icon宽高
    private int width,height;

    // 全参构造
    public JIconDemo(int width, int height) {
        this.width = width;
        this.height = height;
    }
    // 无参构造
    public JIconDemo()  {
    }

    public void init(){
        JIconDemo jIconDemo = new JIconDemo(20,20);
        // 图标可以放按钮上也可以放标签上
        JLabel jLabel = new JLabel("iconDemo",jIconDemo,SwingConstants.CENTER);
        this.setBounds(100,100,500,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();
        container.add(jLabel);

    }


    public static void main(String[] args) {
        new JIconDemo().init();
    }

    // 画icon
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }
}
